package com.future;

import java.util.List;
import java.util.*;

/**
 * Description: 1213. 三个有序数组的交集（思路跟 632. 最小区间 相似）
 *
 * @author weiruibai.vendor
 * Date: 2022/7/5 10:15
 */
public class Solution_1213 {

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5}, arr2 = {1, 2, 5, 7, 9}, arr3 = {1, 3, 4, 5, 8};
        System.out.println(arraysIntersection(arr1, arr2, arr3));
    }

    public static List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
        Info info = new Info();
        info.addLinked(arr1);
        info.addLinked(arr2);
        info.addLinked(arr3);
        while (!info.isEnd) {
            info.removeSmallNode();
        }
        return info.getAns();

    }

    private static class Info {

        private static final Comparator<Node> MY_COMPARATOR = (o1, o2) -> o1.value - o2.value;

        public LinkedList<Node> linkedList;

        private int size;

        private boolean isEnd;

        private List<Integer> result;

        public Info() {
            linkedList = new LinkedList<>();
            result = new ArrayList<>();
        }

        public void addLinked(int[] arr) {
            Node head = new Node(arr[0]);
            Node op = head;
            for (int i = 1; i < arr.length; i++) {
                op.next = new Node(arr[i]);
                op = op.next;
            }
            linkedList.add(head);
            size++;
        }

        public void removeSmallNode() {
            linkedList.sort(MY_COMPARATOR);
            Node last = linkedList.peekLast();
            if (linkedList.get(0).value == last.value) {
                result.add(last.value);
                for (int i = 0; i < linkedList.size(); i++) {
                    Node obj = linkedList.get(i);
                    linkedList.remove(obj);
                    obj = obj.next;
                    if (obj == null) {
                        isEnd = true;
                        return;
                    } else {
                        linkedList.add(i, obj);
                    }
                }
            } else {
                for (int i = 0; i < linkedList.size(); i++) {
                    Node obj = linkedList.get(i);
                    if (obj.value == last.value) {
                        continue;
                    }
                    linkedList.remove(obj);
                    obj = obj.next;
                    if (obj == null) {
                        isEnd = true;
                        return;
                    } else {
                        linkedList.add(i, obj);
                    }
                }
            }
        }

        public boolean isEnd() {
            return isEnd;
        }

        public List<Integer> getAns() {
            return result;
        }

    }

    private static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", next=" + next.value +
                    '}';
        }
    }
}
